<template>
  <div class="exchange-account-management">
    <!-- 搜索及操作按钮区域 -->
    <div class="search-section">
      <el-form :inline="true" :model="searchForm" ref="searchForm">
        <el-form-item>
          <el-input
            v-model="searchForm.keyWords"
            placeholder="输入账户名或交易所名称"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleOpenAccount">开户</el-button>
        </el-form-item>
        <!-- <el-form-item>
          <el-button type="success" @click="handleBindAccount">绑定交易账号</el-button>
        </el-form-item> -->
      </el-form>
    </div>

    <!-- 数据表格 -->
    <el-table
      :data="tableData"
      stripe
      v-loading="tableLoading"
      element-loading-text="正在加载数据..."
       :border="false"
    >
<el-table-column
  label="序号" 
  width="80"
  align="center"
>
  <template #default="scope">
    {{ scope.$index + 1 }} 
  </template>
</el-table-column>      <el-table-column prop="accountName" label="账号名" min-width="120" align="center" ></el-table-column>
      <el-table-column prop="carbonExchangeName" label="交易所" min-width="120" align="center" ></el-table-column>
      <el-table-column prop="createdTime" label="开户时间" min-width="160" align="center" ></el-table-column>
      <el-table-column prop="businessScope" label="业务范围" min-width="400" align="center" >
        <template #default="scope">
          {{ scope.row.businessScope }}
        </template>
      </el-table-column>
      <!-- <el-table-column label="操作" width="80" align="center">
        <template #default="scope">
          <el-button type="text" @click="handleView(scope.row)">查看</el-button>
        </template>
      </el-table-column> -->
    </el-table>

    <!-- 分页 -->
    <div class="pagination-container">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[10]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalCount"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  name: "ExchangeAccountManagement",
  data() {
    return {
      user : JSON.parse(localStorage.getItem('user') || '{}'),
      searchForm: {
        keyWords: "",
        tenantId:""
      },
      tableData: [
       
      ],
      tableLoading: false,
      totalCount: 3, // 示例数据，实际需接口返回
      currentPage: 1,
      pageSize: 10,
    };
  },
  created() {
    this.fetchTableData();
  },
  methods: {
    // 模拟接口请求，实际替换为真实接口逻辑
    async fetchTableData() {
      this.tableLoading = true;
      this.searchForm.tenantId = this.user.tenantId
      
      try {
        const res = await this.$request.post("/assets/exchangeAccount/search", 
          {
            currentPage: this.currentPage,
            pageSize: this.pageSize,
            ...this.searchForm,
          },
        );
        if (res.code === 200) {
          
          this.tableData = res.data.exchangeAccountQueryVoList;
          this.totalCount = res.data.total;
        }
      } catch (error) {
        console.error("获取数据失败:", error);
        this.$message.error("网络错误，请重试");
      } finally {
        this.tableLoading = false;
      }
    },
    handleSearch() {
      this.currentPage = 1;
      this.fetchTableData();
    },
    handleView(row) {
      // 查看逻辑，可跳转详情页或弹窗
      console.log("查看详情:", row);
      // 示例：跳转详情路由
      // this.$router.push({ path: "/exchange/account/detail", query: { id: row.id } });
    },
    handleOpenAccount(row) {
      this.$router.push('/trade/account') 
      // 可补充调用开户接口等逻辑
    },
    handleBindAccount() {
      // 绑定交易账号逻辑
      console.log("执行绑定交易账号操作");
      // 示例：调用绑定接口或弹窗
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.fetchTableData();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.fetchTableData();
    },
  },
};
</script>

<style scoped>
.exchange-account-management {
  padding: 20px;
  background-color: #fff;
  min-height: calc(100vh - 40px);
}

.search-section {
  margin-bottom: 16px;
}

.pagination-container {
  margin-top: 20px;
  text-align: right;
}

.el-table {
  width: 100%;
}
</style>